package com.ldk.Runnable;

import java.io.File;
import java.util.*;

import com.ldk.DB.DBOpt;
import com.ldk.util.MD5;

public class DataDealRunnable2 implements Runnable {

	private List<String> dataBlock;
	private String targetHead;
	private List<String> changeFile;
	
	public DataDealRunnable2(List dataBlock,String targetHead,List changeFile) {
		this.dataBlock = dataBlock;
		this.targetHead = targetHead;
		this.changeFile = changeFile;
	}
	
	@Override
	public void run() {
		try {
			for(String s :dataBlock){
				//除去文件路径前面多余的部分（targetHead）
				String changeS = s.substring(targetHead.length()+1);
				//将路径的\替换成/
				if(changeS.indexOf("\\")!=-1){
					changeS = changeS.replace("\\", "/");
				}
				
				//获取数据库中的MD5码
				DBOpt db = new DBOpt();
				String svnMd5 = null;
				try {
					svnMd5 = db.getMD5(targetHead,changeS);
				} catch (ClassNotFoundException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				//计算本地文件的MD5码
				long start = System.currentTimeMillis();
				String md5 = MD5.getMd5ByFile(new File(s));
				//System.out.println((System.currentTimeMillis()-start)+"ms");
				while(md5.length()<32){
					md5 = "0"+md5;
				}
				md5 = ("$md5 $"+md5).toLowerCase();
				//比对MD5码,判断是否发生变化
				if(svnMd5==null){
					System.out.println(changeS+":");
					changeFile.add(changeS);
				}
				else if(svnMd5!=null && !svnMd5.equals(md5)){
					System.out.println(changeS+":");
					System.out.println("\t原MD5码"+svnMd5+"\r\n\t现MD5码"+md5);
					changeFile.add(changeS);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}
